/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.auxiliares;

import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.mix.data.Pregunta;
import org.grupoavatar.guiaubicua.punto.EncuestaIntroScreen;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;

public class OpcionesListAdapter extends ArrayAdapter<ListaOpcionesRow> {

	Context context;
	int layoutResourceId;
	ListaOpcionesRow data[] = null;
	int indicePregunta;

	public OpcionesListAdapter(Context context, int layoutResourceId,
			ListaOpcionesRow[] data, int indicePregunta) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
		this.indicePregunta = indicePregunta;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ListaOpcionesHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ListaOpcionesHolder();
			holder.opcion = (RadioButton) row.findViewById(R.id.radOpc);

			row.setTag(holder);
		} else {
			holder = (ListaOpcionesHolder) row.getTag();
		}

		holder.opcion
			.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	
				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					Pregunta pregunta = EncuestaIntroScreen.getEncuestaActual().getPreguntas().get(indicePregunta);
					for (int i = 0; i < pregunta.getOpciones().size(); i++) {
						if (i != position)
							pregunta.getOpciones().get(i).desmarcar();
						else
							pregunta.getOpciones().get(i).marcar();
					}
					notifyDataSetChanged();
				}
			});

		holder.opcion.setChecked(EncuestaIntroScreen.getEncuestaActual().getPreguntas().get(indicePregunta).getOpciones().get(position).isMarcado());
		ListaOpcionesRow cat = data[position];
		holder.opcion.setText(cat.opcion);

		return row;
	}

	static class ListaOpcionesHolder {
		RadioButton opcion;
	}
}